package com.wrupple.muba.base.client.module.services.logic;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import com.google.gwt.activity.shared.Activity;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.place.shared.Place;
import com.wrupple.muba.common.domain.DesktopPlace;
import com.wrupple.muba.desktop.client.module.DesktopActivityMapper;

public class DesktopActivityMapperImpl implements DesktopActivityMapper {
	private Map<String, Activity> desktopActivity;
	protected JavaScriptObject applicationHierarchy;

	public DesktopActivityMapperImpl() {
		super();
		desktopActivity = new HashMap<String, Activity>();
	}

	public void addActivity(String id, Activity action) {
		this.desktopActivity.put(id, action);
	}


	@Override
	public Activity getActivity(Place place) {
		DesktopPlace desktopPlace = (DesktopPlace) place;
		//ApplicationItem rootPlace = getRootPlace(desktopPlace);
		Activity a = this.desktopActivity.get(desktopPlace.getActivity());
		if (a == null) {
			return null;
		}else{
			return a;
		}
	}

	public Map<String, Activity> getDesktopActivityMap() {
		return desktopActivity;
	}

	@Override
	public void setDesktopActivityMap(Map<String, Activity> map) {
		this.desktopActivity=map;
	}
	
	public void registerAll(Map<String, Activity> activities, boolean replace) {
		Set<String> ids = activities.keySet();
		if(replace){
			desktopActivity.putAll(activities);
		}else{
			for(String act : ids){
				if(desktopActivity.containsKey(act)){
					//ignore
				}else{
					addActivity(act, activities.get(act));
				}
			}
		}
	}

	@Override
	public void setApplicationHierarchy(JavaScriptObject applicationHierarchy) {
		this.applicationHierarchy=applicationHierarchy;
	}


}
